<template>
  <div class="tag-group">
    <el-tag
      class="tag"
      v-for="item in tagList"
      :id="item.id"
      :key="item.id"
      :effect="item.effect"
      @click="selectTag"
    >
      {{ item.title }}
    </el-tag>
  </div>
</template>

<script>
export default {
  name: 'subTag',
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  components: {},
  data () {
    let list = this.list.map(item => {
      item.effect = 'plain'
      return item
    })
    return {
      tagList: list
    }
  },
  watch: {
    list (curVal, oldVal) {
      let list = curVal.map(item => {
        item.effect = 'plain'
        return item
      })
      if (curVal) {
        this.tagList = list
      }
    }
  },
  created () {
  },
  methods: {
    selectTag (e) {
      let type = 0
      this.tagList = this.tagList.map(item => {
        if (item.id === e.target.id && item.effect === 'plain') {
          item.effect = 'dark'
          type = 1
        } else {
          item.effect = 'plain'
        }
        return item
      })
      this.$emit('click-tag', type ? e.target.id : '')
    }
  }
}
</script>

<style lang='css' scoped>
.tag-group {
  margin-top: 10px;
  margin-bottom: 20px;
}
.tag {
  margin-right: 20px;
  cursor: pointer;
}
</style>
